package org.fast.easy.system.remote.controller;

import cn.hutool.core.io.FileUtil;
import lombok.RequiredArgsConstructor;
import org.fast.easy.core.domain.result.R;
import org.fast.easy.core.util.StringUtils;
import org.fast.easy.core.util.file.MimeTypeUtils;
import org.fast.easy.framework.log.annotation.Log;
import org.fast.easy.framework.log.enums.BusinessType;
import org.fast.easy.framework.satoken.utils.LoginHelper;
import org.fast.easy.framework.web.core.BaseController;
import org.fast.easy.oss.domain.vo.OriginalFile;
import org.fast.easy.oss.domain.vo.RemoteFile;
import org.fast.easy.oss.feign.service.OssFeignService;
import org.fast.easy.system.api.SysProfileApi;
import org.fast.easy.system.upms.domain.vo.AvatarVo;
import org.fast.easy.system.upms.service.ISysUserService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

/**
 * 个人信息 业务处理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user/profile")
public class RemoteSysProfileController extends BaseController implements SysProfileApi {

    private final ISysUserService userService;

    /**
     * 远程调用Feign
     */
    private final OssFeignService ossFeignService;

    /**
     * 头像上传
     *
     * @param avatarfile 用户头像
     */
    // @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<AvatarVo> avatar(@RequestPart("avatarfile") MultipartFile avatarfile) throws IOException {
        if (!avatarfile.isEmpty()) {
            String extension = FileUtil.extName(avatarfile.getOriginalFilename());
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            OriginalFile file = new OriginalFile(avatarfile.getName(), avatarfile.getOriginalFilename(), avatarfile.getContentType(), avatarfile.getBytes());
            R<RemoteFile> remoteResult = ossFeignService.upload(file);
            if (StringUtils.isNotNull(remoteResult) || StringUtils.isNotNull(remoteResult.getData())) {
                if (R.SUCCESS == remoteResult.getCode()) {
                    RemoteFile  oss = remoteResult.getData();
                    String avatar = oss.getUrl();
                    if (userService.updateUserAvatar(LoginHelper.getUserId(), oss.getOssId())) {
                        AvatarVo avatarVo = new AvatarVo();
                        avatarVo.setImgUrl(avatar);
                        return R.ok(avatarVo);
                    }
                }else{
                    return R.fail("上传图片异常，请联系管理员");
                }
            }
        }
        return R.fail("上传图片异常，请联系管理员");
    }
}
